package com.intelligentmeterreadingsys.otherequipment.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.android.rfid.RfidHelper;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.otherequipment.db.EquipementDao;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;
import com.intelligentmeterreadingsys.povo.TypePoVo;
import com.intelligentmeterreadingsys.util.BaiDuGpsUtil;
import com.intelligentmeterreadingsys.util.BluetoothHelp;
import com.intelligentmeterreadingsys.util.DateUtil;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 设备更换
 * 
 * @author king
 * 
 */
public class EquipementReplaceActivity extends Activity implements
		OnClickListener,BDLocationListener {
	public TextView textview1;// 经度
	public TextView textview2;// 维度
	public TextView textview3;// 旧设备编号
	public TextView textview4;// Rfid编号
	public EditText edit1;// 新设备编号
	public EditText edit2;// 安装地址
	public EditText edit3;// 设备描述
	public EditText edit4;// 精确地址
	public EditText edit5;// 更换原因
	public Spinner spinner;// 设备类型
	public Button button1;// 读卡
	public Button button2;// 定位
	public Button button3;// 保存
	public Button button4;// 上传

	public String type;// 类型名称
	public String typeid = "0";
	public Double Latitude;// 维度
	public Double Longitude;// 经度
	public List<TypePoVo> list;// 类型集合
	public String[] date = null;
	public ArrayList<String> imagepath;
	private final static int DIALOG = 1;
	private static final int REQUEST_CODE_TAKE_IMAGE = 1;
	public String locationtype = "gps";
	public static final int ITEM0 = Menu.FIRST;// 系统值
	public LocationClient client;
	public long time;
	public ProgressDialog pd;
	public OtherEquipmentPOVO oldereq=null;
	public OtherEquipmentPOVO eq = null;
	public String id = "0";
	public boolean ismodify = false;
	public String RfidNumber = "";
	public String xieyi = "15693";
	public RfidHelper rh;// 读卡帮助对象
	public String readtype = "终端读卡";

	public BluetoothHelp bluehelp;
	public String[] readdate;
	public boolean isbluetooth = true;
	public boolean isStop = false;
	public boolean isSave = false;
	public String url = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.other_equipment_replace);
		id=getIntent().getStringExtra("equipementId");
		init();

	}

	// 初始化控件
	public void init() {
		textview1 = (TextView) findViewById(R.id.other_equipment_replace_longitude);
		textview2 = (TextView) findViewById(R.id.other_equipment_replace_latitude);
		textview3 = (TextView) findViewById(R.id.other_equipment_replace_ordernumber);
		textview4 = (TextView) findViewById(R.id.other_equipment_replace_rfidnumber);
		edit1 = (EditText) findViewById(R.id.other_equipment_replace_newnumber);
		edit2 = (EditText) findViewById(R.id.other_equipment_replace_adds);
		edit3 = (EditText) findViewById(R.id.other_equipment_replace_desc);
		edit4 = (EditText) findViewById(R.id.other_equipment_replace_locationadds);
		edit5 = (EditText) findViewById(R.id.other_equipment_replace_reason);
		button1 = (Button) findViewById(R.id.other_equipment_replace_readcard);
		button2 = (Button) findViewById(R.id.other_equipment_replace_latitude);
		button3 = (Button) findViewById(R.id.other_equipment_replace_save);
		button4 = (Button) findViewById(R.id.other_equipment_replace_upload);
		spinner = (Spinner) findViewById(R.id.other_equipment_replace_type);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		bluehelp=new BluetoothHelp(this);
		bluehelp.init();
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				type = arg0.getItemAtPosition(arg2).toString();
				if (arg2 != 0) {
					typeid = list.get(arg2 - 1).getNumber();
				}

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				type = "无";
				typeid = "0";

			}

		});
		EquipementDao dao = new EquipementDao(this);

		list = dao.queryOrderType();
		oldereq=dao.queryOne(id, null);
		if (ismodify) {
			getTypePosition(type);
		}

		if (list != null && list.size() > 0) {
			date = new String[list.size() + 1];
			if (!ismodify) {
				date[0] = "请选择";
			}
			for (int i = 0; i < list.size(); i++) {
				date[i + 1] = list.get(i).getName();
			}
		} else {

			date = new String[] { "请选择" };
		}
		dao.close();
		ArrayAdapter<String> ada = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, date);// 放数据
		ada.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(ada);
		date = new String[] { "gps", "netowrke" };
		client = new LocationClient(this);
		client.registerLocationListener(this);
		initlocation();
		initRFID(2);
		viewdate(oldereq);
		getTypePosition(oldereq.getTypename());
		
	}

	/**
	 * 定位初始化
	 */
	public void initlocation() {

		LocationClientOption option = BaiDuGpsUtil
				.getLocationClientOption(locationtype);
		client.setLocOption(option);

	}

	@Override
	public void onClick(View v) {

	}

	@Override
	public void onReceiveLocation(BDLocation location) {
		getlocation(location);
		
	}

	@Override
	public void onReceivePoi(BDLocation arg0) {
		// TODO Auto-generated method stub
		
	}
	/**
	 * 判断GPS是否存在或开启
	 */
	public boolean openGPSSettings() {
		LocationManager alm = (LocationManager) getSystemService(this.LOCATION_SERVICE);

		if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {

			return true;
		}
		Toast.makeText(this, "请开启GPS！", Toast.LENGTH_SHORT).show();
		Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
		startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面
		return false;
	}

	/**
	 * 定位
	 * 
	 * @param location
	 */
	public void getlocation(BDLocation location) {
		Date d = new Date();
		if (d.getTime() - time > 360000) {
			Toast.makeText(EquipementReplaceActivity.this, "定位超时",
					Toast.LENGTH_SHORT).show();
			textview1.setText("纬度:0.0");
			textview2.setText("经度:0.0");
			this.Latitude = 0.0;
			this.Longitude = 0.0;
			pd.dismiss();
			client.stop();
		}
		if (location == null) {
			return;
		}

		if (locationtype.equals("gps") && location.getLocType() == 61) {
			textview1.setText("纬度:" + location.getLatitude());
			textview2.setText("经度:" + location.getLongitude());
			this.Latitude = location.getLatitude();
			this.Longitude = location.getLongitude();
			Toast.makeText(EquipementReplaceActivity.this, "定位成功",
					Toast.LENGTH_SHORT).show();
			client.stop();
			pd.dismiss();
		} else if (locationtype.equals("network")
				&& location.getLocType() == 161) {
			textview1.setText("纬度:" + location.getLatitude());
			textview2.setText("经度:" + location.getLongitude());
			this.Latitude = location.getLatitude();
			this.Longitude = location.getLongitude();
			Toast.makeText(EquipementReplaceActivity.this, "定位成功",
					Toast.LENGTH_SHORT).show();
			client.stop();
			pd.dismiss();
		}

	}
	
	@Override
	protected void onDestroy() {
		if (client != null && client.isStarted()) {
			client.stop();
		}
		if(bluehelp!=null){
			bluehelp.stop();
		}
	
		super.onDestroy();
	}

	/**
	 * 修改时交换类型位置
	 * 
	 * @param type
	 * @return
	 */
	public int getTypePosition(String type) {
		if (list != null && list.size() > 0) {
			for (int k = 0; k < list.size(); k++) {
				if (list.get(k).getName().equals(type)) {
					spinner.setSelection(k + 1, true);
					return k;
				}
			}
		}
		return 0;

	};

	/**
	 * 初始化读卡
	 */
	public void initRFID(int type) {
		rh = RfidHelper.getInstance();
		rh.setCardType(type);
		rh.open();
		rh.setAllNoOfTimes(1);
		rh.setTimeInterval(50);
		// 设置激活读卡器后等待的时间.sdk默认10
		rh.setPingTimeInterval(10);
	}

	/**
	 * 当RFID已绑定设备时调用
	 */
	public void initmodify() {
		this.Latitude = eq.getLatitude();
		this.Longitude = eq.getLongitude();
		this.RfidNumber = eq.getRfidNumber();
		this.type = eq.getTypename();
		getTypePosition(type);
	}

	public void viewdate(OtherEquipmentPOVO eq) {
		edit1.setText(eq.getNumber());
		edit2.setText(eq.getAdds());
		edit3.setText(eq.getLocationadds());
		edit4.setText(eq.getDesc());
		textview4.setText(eq.getRfidNumber());
		textview1.setText("维度:" + eq.getLatitude());
		textview2.setText("经度:" + eq.getLongitude());
	}
   
	
	//保存更换设备信息
	public boolean save(){
		boolean b=false;
		ContentValues values=new ContentValues();
		values.put("ep_checktype", "更换");
		values.put("ep_checkdesc", eq.getCheckdesc()+"此设备被编号"+edit1.getEditableText().toString()+"更换");
		values.put("ep_checkdate", DateUtil.getBeforeDay(0));
		return b;
	}
}
